package com.cidp.system.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.DispatchAction;
import org.extremecomponents.table.limit.Limit;

import com.cidp.system.model.TdictParam;
import com.cidp.system.pojo.DictParamPo;
import com.cidp.system.service.IDictParamService;
import com.litt.core.common.Constants;
import com.litt.core.page.IPageList;
import com.litt.core.page.PagePoParam;
import com.litt.core.util.ExtremeTableUtil;

/**
 * <b>标题：</b>参数字典.
 * <pre><b>描述</b>
 *    参数字典管理
 * </pre>
 * 
 * @author lijian
 * @since 2008-6-3
 * @version 1.0
 */
public class DictParamAction extends DispatchAction{

	private IDictParamService dictParamService;
	
	/**
	 * 查看参数字典
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward search(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		DynaActionForm dynaActionForm=(DynaActionForm) form;
		DictParamPo dictParamPo = (DictParamPo)dynaActionForm.get("dictParamPo");
		
		Limit limit=ExtremeTableUtil.getLimit(request,Constants.DEFAULT_PAGE_SIZE);
		IPageList page = dictParamService.listByPage(new PagePoParam(dictParamPo, limit.getPage(), limit.getCurrentRowsDisplayed()));
		
		for(int i=0;i<page.getRsList().size();i++){
			TdictParam dictParam = (TdictParam) page.getRsList().get(i);
			if(dictParam.getStatus().intValue() == 0)
				dictParam.setStatusName("正常");
			else if(dictParam.getStatus().intValue() == 1)
				dictParam.setStatusName("屏蔽");
			else
				dictParam.setStatusName("不可修改");
		}
		request.setAttribute("resultList", page.getRsList());
		request.setAttribute("totalRows", new Integer(page.getTotalSize()));

		return mapping.findForward("toList");
	}
	
	/**
	 * 保存参数字典
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward save(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		DynaActionForm dynaActionForm=(DynaActionForm) form;
		DictParamPo dictParamPo = (DictParamPo)dynaActionForm.get("dictParamPo");
		
		TdictParam dictParam = new TdictParam();
		dictParam.setDictContent(dictParamPo.getDictContent());
		dictParam.setDictType(dictParamPo.getDictType());
		dictParam.setDictValue(dictParamPo.getDictValue());
		dictParam.setRemark(dictParamPo.getRemark());
		dictParam.setStatus(new Long(0));
		
		dictParamService.saveDictParam(dictParam);
		
		request.setAttribute("successFlag","dictParam_saveDictParam");
		return mapping.findForward("toSave");
	}
	
	/**
	 * 新增参数字典
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward add(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		return mapping.findForward("toAdd");
	}
	
	/**
	 * 修改参数字典
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward edit(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		DynaActionForm dynaActionForm=(DynaActionForm) form;
		DictParamPo dictParamPo = (DictParamPo)dynaActionForm.get("dictParamPo");
		String dictId = request.getParameter("id");
		TdictParam dictParam = dictParamService.loadById(new Long(dictId));
		try{
			BeanUtils.copyProperties(dictParamPo, dictParam);
		}catch(Exception e){
			
		}
		dynaActionForm.set("dictParamPo", dictParamPo);
		return mapping.findForward("toEdit");
	}
	
	/**
	 * 更新参数字典
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward update(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		DynaActionForm dynaActionForm=(DynaActionForm) form;
		DictParamPo dictParamPo = (DictParamPo)dynaActionForm.get("dictParamPo");
		TdictParam dictParam = dictParamService.loadById(dictParamPo.getSerialNo());
		dictParam.setDictContent(dictParamPo.getDictContent());
		dictParam.setDictType(dictParamPo.getDictType());
		dictParam.setDictValue(dictParamPo.getDictValue());
		dictParam.setRemark(dictParamPo.getRemark());
		dictParam.setStatus(dictParamPo.getStatus());
		
		dictParamService.updateDictParam(dictParam);
		
		request.setAttribute("successFlag","dictParam_updateDictParam");
		return mapping.findForward("toUpdate");
	}

	public void setDictParamService(IDictParamService dictParamService) {
		this.dictParamService = dictParamService;
	}
}
